#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "DHT11.h"
#include "stdio.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
#include <string.h>

#include "stm32f10x.h"


uint8_t temperature = 0;
uint8_t humidity = 0;

int8_t minutes = 0;
int8_t seconds = 0;

uint8_t mode_flag = 1;

uint8_t timer_isEnable = 0;

uint8_t lastButtonState_1 = 1; 
uint8_t currentButtonState_1 = 1;

uint8_t lastButtonState_2 = 1; 
uint8_t currentButtonState_2 = 1;

uint8_t lastButtonState_3 = 1; 
uint8_t currentButtonState_3 = 1;

uint8_t lastButtonState_4 = 1; 
uint8_t currentButtonState_4 = 1;

// 数字0-9的段码表（a,b,c,d,e,f,g,dp）
const uint8_t digitSegments[10] = {
    0x3F, // 0 (a,b,c,d,e,f)
    0x06, // 1 (b,c)
    0x5B, // 2 (a,b,d,e,g)
    0x4F, // 3 (a,b,c,d,g)
    0x66, // 4 (b,c,f,g)
    0x6D, // 5 (a,c,d,f,g)
    0x7D, // 6 (a,c,d,e,f,g)
    0x07, // 7 (a,b,c)
    0x7F, // 8 (a,b,c,d,e,f,g)
    0x6F  // 9 (a,b,c,f,g)
};

uint8_t txBuffer[8];
struct state
{
    uint8_t digit;      // 当前显示的数字 (0-9)
    uint8_t dp_state;   // 小数点状态 (0:灭, 1:亮)
};

struct state digit_state[4] = {
    {0, 0}, // 第1位
    {0, 0}, // 第2位
    {0, 0}, // 第3位
    {0, 0}  // 第4位
};

//a8 a9 a10 a11    -> 1 2 3 4
//a4 a5 a6 a7      -> a b c d
//b12 b13 c15 c14  -> e f g .

void RCC_Configuration_send_data(void);
void GPIO_Configuration_send_data(void);
void USART2_Configuration_send_data(void);
void TM_Init(void);
void DisplayDigit1(uint8_t number, uint8_t show_dp);
void DisplayDigit2(uint8_t number, uint8_t show_dp);
void DisplayDigit3(uint8_t number, uint8_t show_dp);
void DisplayDigit4(uint8_t number, uint8_t show_dp);
void DisplayAllDigits(uint8_t num1, uint8_t num2, uint8_t num3, uint8_t num4);
void TIM3_Init(void);
void TIM2_Init(void);
void GPIO_Init_A12(void);
void Button_Init(void);

void USART1_Init(void) 
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    // 1. 开启时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

    // 2. 配置 PA9 (TX) 为复用推挽输出
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // 3. 配置 PA10 (RX) 为浮空输入
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // 4. 配置 USART1 参数
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

    USART_Init(USART1, &USART_InitStructure);

    // 5. 使能 USART1
    USART_Cmd(USART1, ENABLE);
}

// 发送一个字符（通过USART1）
void USART1_SendChar(char ch) 
{
    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
		USART_SendData(USART1, (uint8_t)ch);
}

// 发送字符串
void USART1_SendString(const char* str) 
{
    while (*str) 
	{
        USART1_SendChar(*(str++));
    }
}

// 接收一个字符（阻塞，直到收到）
char USART1_ReceiveChar(void) 
{
    while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
    return (char)USART_ReceiveData(USART1);
}

// 接收多个字符到缓冲区（阻塞，直到接收指定数量）
void USART1_ReceiveString(uint8_t *buffer, uint16_t len) 
{
    for (uint16_t i = 0; i < len; i++) 
	{
        buffer[i] = USART1_ReceiveChar();
    }
    buffer[len] = '\0'; // 添加字符串结束符
}

// 检查字符串中是否包含 "OK"
int Is_OK_In_Response(char *response) 
{
    return strstr(response, "OK") != NULL;
}

const char json_template[] = "{\"temperature\":%d,\"humidity\":%d,\"speed\":%d,\"status\":%d}";
char json_buffer[100];  // 足够容纳JSON字符串的缓冲区

// 填充数值并发送的函数
void send_sensor_data(int temp, int humi, int spd, int stat)
{
    
    printf("发送数据: %s\n", json_buffer);  // 调试用
}

int main(void) 
{
	USART1_Init();
    // 初始化系统时钟
    //RCC_Configuration_send_data();
    // 初始化GPIO
    //GPIO_Configuration_send_data();
    // 初始化USART2
    //USART2_Configuration_send_data();
	//TIM3_Init();
	//TIM2_Init();
	OLED_Init();
	//TM_Init();
	//SystemInit();
	OLED_Clear();
	//Button_Init();
	GPIO_Init_A12();
	
	char response[128] = {0};
	USART1_SendString("AT+CIPSTART=\"TCP\",\"192.168.247.205\",12345\r\n");
	USART1_ReceiveString((uint8_t*)response, 6);
	//OLED_ShowString(1, 1, response);
	USART1_SendString("ATE0\r\n");	

	
	/*
	"temperature:"%d
	humidity%d
	speed%d
	status%d
	*/
	
    while (1) 
	{
	/*
		USART1_SendString("AT+CIPSEND=10\r\n");
		Delay_ms(1000);
		USART1_SendString("humidity+20\r\n");
		Delay_ms(1000);
		USART1_SendString("AT+CIPSEND=16\r\n");
		Delay_ms(1000);
		USART1_SendString("temperature+22\r\n");
		Delay_ms(1000);
	*/	
		
		
		
		/*
		USART1_SendString("AT+CIPSEND=16\r\n");
		Delay_ms(1000);
		USART1_SendString("temperature+20\r\n");
		Delay_ms(1000);
		USART1_SendString("AT+CIPSEND=10\r\n");
		Delay_ms(1000);
		USART1_SendString("humidity+22\r\n");
		Delay_ms(1000);
		*/
		/*
		uint8_t num1 = (minutes / 10) % 10;
		uint8_t num2 = minutes % 10;
		uint8_t num3 = (seconds / 10) % 10;
		uint8_t num4 = seconds % 10;
		*/
		
		//DisplayAllDigits(num1, num2, num3, num4);
		DHT11_ReadData(&temperature, &humidity);
		Delay_ms(3000);
		OLED_ShowString(1, 1, "temp:");
		OLED_ShowString(2, 1, "humi:");
		//OLED_ShowString(3, 1, "mode:");
		//OLED_ShowString(4, 1, "min:");
		//OLED_ShowString(4, 9, "sec:");
		OLED_ShowNum(1, 6, temperature, 2);
		OLED_ShowNum(2, 6, humidity, 2);
		//OLED_ShowNum(3, 6, mode_flag, 1);
		//OLED_ShowNum(4, 5, minutes, 2);
		//OLED_ShowNum(4, 13, seconds, 2);
        
        // 发送数据
		// 复制浮点数据到缓冲区
		//memcpy(txBuffer, &temperature, 4);
		//memcpy(txBuffer + 4, &humidity, 4);
		
		/*
		USART1_SendString("AT+CIPSEND=16\r\n");
		Delay_ms(1000);
		USART1_SendString("temperature+22\r\n");
		Delay_ms(1000);
		*/
		
		
		USART1_SendString("AT+CIPSEND=56\r\n");
		Delay_ms(100);
		
		// 用实际数值填充占位符
		snprintf(json_buffer, sizeof(json_buffer), json_template, temperature, humidity, 22, 1);
		strncat(json_buffer, "\r\n", sizeof(json_buffer) - strlen(json_buffer) - 1);
		
		USART1_SendString(json_buffer);  // 需自行实现该函数
		Delay_ms(100);
		
		
		/*
		char temperature_string[10];
		sprintf(temperature_string, "%2d", temperature);
		
		char basic_message[50] = "temperature+";
		strcat(basic_message, temperature_string);
		strcat(basic_message, "\r\n");
		
		USART1_SendString(basic_message);
		Delay_ms(1000);
*/
/*
		OLED_ShowString(3, 1, basic_message);
		
		char humidity_string[10];
		
		sprintf(humidity_string, "%d", humidity);
		char basic2_message[50] = "humidity+";
		strcat(basic2_message, humidity_string);
		
		
		
		OLED_ShowString(4, 1, basic2_message);
		
		
		//for (int i = 0; i < 8; i++) 
		//{
			//USART_SendData(USART1, txBuffer[i]);
			//while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
		//}    
        
        //延时2秒
       // for (int i = 0; i < 2000000; i++); // 简单延时
*/		
		
    }
	
	
}

// TIM3中断服务函数
void TIM3_IRQHandler(void) 
{
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) 
	{
		if(timer_isEnable == 1)
		{
			if(seconds > 0)
			{
				seconds--;
			}
			else if(seconds == 0)
			{
				minutes--;
				seconds = 59;
			}
		}
		if(seconds == 0 && minutes == 0 && timer_isEnable == 1)
		{
			timer_isEnable = 0;
			
			GPIO_SetBits(GPIOA, GPIO_Pin_12);
			for(int i = 0; i < 1000000; i++);
			GPIO_ResetBits(GPIOA, GPIO_Pin_12);
		}
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    }
}

// TIM2中断服务函数
void TIM2_IRQHandler(void) 
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) 
	{
		// 1. 同步状态：当前状态变为上次状态
        lastButtonState_1 = currentButtonState_1;
		lastButtonState_2 = currentButtonState_2;
		lastButtonState_3 = currentButtonState_3;
		lastButtonState_4 = currentButtonState_4;
        
        // 2. 读取新状态（按键按下为0，抬起为1）
        currentButtonState_1 = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11);
        currentButtonState_2 = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10);
		currentButtonState_3 = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1);
		currentButtonState_4 = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0);
        // 3. 检测完整动作：上次按下(0) -> 当前抬起(1)
        if (lastButtonState_1 == 0 && currentButtonState_1 == 1) 
		{
           if(mode_flag == 1)
			{
				if(minutes <= 59)
				{
					minutes++;
				}
				else
				{
					minutes = 0;
				}	
			}
			else if(mode_flag == 2)
			{
				if(seconds < 59)
				{
					seconds++;
				}
				else
				{
					seconds = 0;
					minutes++;
				}
			}
        }
		else if(lastButtonState_2 == 0 && currentButtonState_2 == 1)
		{
			if(mode_flag == 1)
			{
				if(minutes == 0)
				{
					minutes = 60;
				}
				else
				{
					minutes--;
				}
			}
			else if(mode_flag == 2)
			{
				if(seconds == 0)
				{
					seconds = 59;
					minutes--;
				}
				else 
				{
					seconds--;
				}
			}
		}
		else if(lastButtonState_3 == 0 && currentButtonState_3 == 1)
		{
			if(mode_flag == 1)
			{
				mode_flag = 2;
			}
			else if(mode_flag == 2)
			{
				mode_flag = 1;
			}
		}
		else if(lastButtonState_4 == 0 && currentButtonState_4 == 1)
		{
			timer_isEnable = 1;
		}
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    }
}

// 定时器3初始化 - 生成1秒定时中断
void TIM3_Init(void) 
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    // 使能TIM3时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    // TIM3配置 - 1秒中断
    TIM_TimeBaseStructure.TIM_Period = 7199;           // 自动重装载值
    TIM_TimeBaseStructure.TIM_Prescaler = 9999;        // 预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;       // 时钟分割
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  // 向上计数
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    // 使能TIM3中断
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM3, ENABLE);

    // 配置NVIC
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

void TIM2_Init(void) 
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    // 使能TIM2时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    // TIM2配置 - 20ms中断
    TIM_TimeBaseStructure.TIM_Period = 1439;             // 自动重装载值
    TIM_TimeBaseStructure.TIM_Prescaler = 999;           // 预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;         // 时钟分割
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  // 向上计数
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    // 使能TIM2中断
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);

    // 配置NVIC
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

// 初始化GPIO
void TM_Init(void) 
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    // 使能GPIO时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);

    // 配置位选引脚 (PA8-PA11)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // 配置段选引脚
    // PA4-PA7 (a,b,c,d)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // PB12-PB13 (e,f)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    // PC14-PC15 (g,dp)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    // 初始关闭所有数码管
    GPIO_ResetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11);
}

void RCC_Configuration_send_data(void)
{
    // 使能GPIOA和USART2时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}

void GPIO_Configuration_send_data(void) 
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    // 使能USART1和GPIOA的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
    
    // 配置TX引脚（PA9）为复用推挽输出
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  // 复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // 如果需要接收功能，还需配置RX引脚（PA10）为浮空输入
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  // 浮空输入
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void USART2_Configuration_send_data(void) 
{
    USART_InitTypeDef USART_InitStructure;
    
    USART_InitStructure.USART_BaudRate = 11500;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx;
    
    USART_Init(USART1, &USART_InitStructure);  // 初始化USART1
    USART_Cmd(USART1, ENABLE);  // 使能USART1
}

// 显示第1位数码管（PA8）
void DisplayDigit1(uint8_t number, uint8_t show_dp) 
{
    // 关闭所有位选
    GPIO_ResetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11);
    
    // 获取段码
    uint8_t seg = digitSegments[number % 10];
    
    // 设置段选（低电平点亮）
    // a段 (PA4)
    GPIO_WriteBit(GPIOA, GPIO_Pin_4, (seg & 0x01) ? Bit_RESET : Bit_SET);
    // b段 (PA5)
    GPIO_WriteBit(GPIOA, GPIO_Pin_5, (seg & 0x02) ? Bit_RESET : Bit_SET);
    // c段 (PA6)
    GPIO_WriteBit(GPIOA, GPIO_Pin_6, (seg & 0x04) ? Bit_RESET : Bit_SET);
    // d段 (PA7)
    GPIO_WriteBit(GPIOA, GPIO_Pin_7, (seg & 0x08) ? Bit_RESET : Bit_SET);
    // e段 (PB12)
    GPIO_WriteBit(GPIOB, GPIO_Pin_12, (seg & 0x10) ? Bit_RESET : Bit_SET);
    // f段 (PB13)
    GPIO_WriteBit(GPIOB, GPIO_Pin_13, (seg & 0x20) ? Bit_RESET : Bit_SET);
    // g段 (PC15)
    GPIO_WriteBit(GPIOC, GPIO_Pin_14, (seg & 0x40) ? Bit_RESET : Bit_SET);
    // dp点 (PC14) - 默认关闭
    GPIO_WriteBit(GPIOC, GPIO_Pin_15, Bit_SET);
    
    // 开启第1位
    GPIO_SetBits(GPIOA, GPIO_Pin_8);
	
	digit_state[0].digit = number % 10;
    digit_state[0].dp_state = show_dp ? 1 : 0;
}

// 显示第2位数码管（PA9）
void DisplayDigit2(uint8_t number, uint8_t show_dp) 
{
    GPIO_ResetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11);
    uint8_t seg = digitSegments[number % 10];
    
    GPIO_WriteBit(GPIOA, GPIO_Pin_4, (seg & 0x01) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOA, GPIO_Pin_5, (seg & 0x02) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOA, GPIO_Pin_6, (seg & 0x04) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOA, GPIO_Pin_7, (seg & 0x08) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOB, GPIO_Pin_12, (seg & 0x10) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOB, GPIO_Pin_13, (seg & 0x20) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOC, GPIO_Pin_14, (seg & 0x40) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOC, GPIO_Pin_15, Bit_RESET);
    
    GPIO_SetBits(GPIOA, GPIO_Pin_9);
	digit_state[1].digit = number % 10;
	digit_state[1].dp_state = show_dp ? 1 : 0;
}

// 显示第3位数码管（PA10）
void DisplayDigit3(uint8_t number, uint8_t show_dp) 
{
    GPIO_ResetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11);
    uint8_t seg = digitSegments[number % 10];
    
    GPIO_WriteBit(GPIOA, GPIO_Pin_4, (seg & 0x01) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOA, GPIO_Pin_5, (seg & 0x02) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOA, GPIO_Pin_6, (seg & 0x04) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOA, GPIO_Pin_7, (seg & 0x08) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOB, GPIO_Pin_12, (seg & 0x10) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOB, GPIO_Pin_13, (seg & 0x20) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOC, GPIO_Pin_14, (seg & 0x40) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOC, GPIO_Pin_15, Bit_SET);
    
    GPIO_SetBits(GPIOA, GPIO_Pin_10);
	digit_state[2].digit = number % 10;
	digit_state[2].dp_state = show_dp ? 1 : 0;
}


// 显示第4位数码管（PA11）
void DisplayDigit4(uint8_t number, uint8_t show_dp) 
{
    GPIO_ResetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11);
    uint8_t seg = digitSegments[number % 10];
    
    GPIO_WriteBit(GPIOA, GPIO_Pin_4, (seg & 0x01) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOA, GPIO_Pin_5, (seg & 0x02) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOA, GPIO_Pin_6, (seg & 0x04) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOA, GPIO_Pin_7, (seg & 0x08) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOB, GPIO_Pin_12, (seg & 0x10) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOB, GPIO_Pin_13, (seg & 0x20) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOC, GPIO_Pin_14, (seg & 0x40) ? Bit_RESET : Bit_SET);
    GPIO_WriteBit(GPIOC, GPIO_Pin_15, Bit_RESET);
    
    GPIO_SetBits(GPIOA, GPIO_Pin_11);
	digit_state[3].digit = number % 10;
	digit_state[3].dp_state = show_dp ? 1 : 0;
}

// 动态扫描显示函数（需在循环中调用）
void DisplayAllDigits(uint8_t num1, uint8_t num2, uint8_t num3, uint8_t num4) 
{
    DisplayDigit2(num1, 0);
    Delay_ms(2);
	DisplayDigit3(num3, 1);
    Delay_ms(2);
	DisplayDigit4(num4, 0);
    Delay_ms(2);
    DisplayDigit1(num1, 0);
	Delay_ms(2);  
}

void GPIO_Init_A12(void) 
{
    // 使能GPIOA和GPIOC的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    
    // 配置GPIO结构体
    GPIO_InitTypeDef GPIO_InitStructure;
    
    // 配置PA7（推挽输出，50MHz）
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    // 推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  // 输出速度50MHz
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // 设置初始状态（可选）
    GPIO_ResetBits(GPIOC, GPIO_Pin_12);  // PA7初始化为低电平  
}

void Button_Init(void) 
{ 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);   
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10| GPIO_Pin_1 | GPIO_Pin_0 | GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}

